package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.teaching.TeachingSwitch;
import vn.edu.saigontech.ncms.service.TeachingSwitchService;
import vn.edu.saigontech.ncms.util.DateUtil;

@SuppressWarnings("unchecked")
public class TeachingSwitchServiceImpl extends HibernateGenericService<TeachingSwitch, Long> implements	TeachingSwitchService {

	@Override
	public Collection<TeachingSwitch> findByDate(Date from, Date to) {
		Collection<TeachingSwitch> all = this.findAll();
		Collection<Date> range = DateUtil.sequence(from, to);
		Collection<TeachingSwitch> result = new LinkedHashSet<TeachingSwitch>();
		for (TeachingSwitch each : all) {
			Collection<Date> dates = DateUtil.sequence(each.getFromDate(), each.getToDate());
			dates.retainAll(range);
			if (!dates.isEmpty()) {
				result.add(each);
			}
		}
		return result;
	}

	@Override
	public Collection<TeachingSwitch> findBySwitchTeacher(Long teacherId) {
		Criteria criteria = this.createCriteria();
		criteria.createCriteria("inplaceTeacher").add(
				Restrictions.eq("id", teacherId));
		return criteria.list();
	}	
}